
#include "lib_image.h"
//****************************************************************

static  pthread_t  lib_image_thread;
static  int        lib_image_index = 1;
//****************************************************************
int lib_image_show(int lcd_type)
{
	int ret = -1;

	char filename[1024] = {0};

	//****************************************************************
	
	memset(filename,0,sizeof(filename));
	ret = file_photo_get_fname(filename,lib_image_index);

#if 0
	if(!ret){
		log_i("i:t:n=%04d:%04d:%s\n",lib_image_index,file_photo_get_total(),filename);
	}
#endif
	//****************************************************************

#if 1
	lib_image_index++;
	if(lib_image_index > file_photo_get_total()){
		lib_image_index = 1;
	}	
	
	//srand(lib_image_index);
	
	//log_i("r=%04d\n",rand());

	lib_image_index = rand()%file_photo_get_total();

	//log_i("i=%04d\n",lib_image_index);
	
#elif 1

	lib_image_index = rand()%file_photo_get_total();

#endif
	//****************************************************************
	
	if(ret != 0){
		return -1;
	}
	
	if(!__Valid_PhotoFileType(filename)){
		return -1;
	}
	//****************************************************************
	if(lcd_type == 0)
	{
		decode_and_show_jpeg_0(filename);
	}
	
	if(lcd_type == 1)
	{
		decode_and_show_jpeg_1(filename);
	}
	
	//****************************************************************

	return 0;
}


//****************************************************************
void* lib_image_show_task(void *arg)
{
	int ret = -1;
	struct timeval timer;	

	while(1)
	{
		//****************************************************************

		//ret = lib_image_show(0);
		ret = lib_image_show(1);
		if(ret != 0){
			continue;
		}
		//****************************************************************
		timer.tv_sec  = 6;
		timer.tv_usec = 0;
		
		switch(select(0, NULL, NULL, NULL, &timer)) 
		{
		case -1:  
			log_e("Error!\n");
			break;
		case 0: 
			break;
		default:break;
		}
		
		//****************************************************************

		usleep(1000);

		//****************************************************************
	}
}

//****************************************************************

int lib_image_init(void)
{
	int ret  = 0;
	
	log_i("lib image init.\n");

	ret=pthread_create(&lib_image_thread, NULL, lib_image_show_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
		return -1;
	}



	return 0;
}


int lib_image_deinit(void)
{

	



	return 0;
}

//****************************************************************

